package com.qidian.mes.machine.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qidian.common.pojo.MESResult;
import com.qidian.mes.machine.mapper.MachineLedgerMapper;
import com.qidian.mes.machine.service.MachineLedgerService;
import com.qidian.mes.pojo.Ledger;
import com.qidian.mes.pojo.MachineType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.rmi.dgc.Lease;
import java.util.List;

/**
 * @Author 任超
 * Time 2024/10/17 13:40
 */
@Service
public class MachineLedgerServiceImpl implements MachineLedgerService {

    @Autowired
    private MachineLedgerMapper machineLedgerMapper;

    @Override
    public MESResult selectList(Ledger ledger, int page, int limit) {
        PageHelper.startPage(page,limit);
        List<Ledger> ledgers = machineLedgerMapper.selectList(ledger);
        PageInfo<Ledger> pageInfo = new PageInfo<>(ledgers);
        return MESResult.success(pageInfo);
    }

    @Override
    public MESResult addLedger(Ledger ledger) {
        if (machineLedgerMapper.selectByCode(ledger.getMachineCode())!= null )
            return MESResult.error();
        if (machineLedgerMapper.addLedger(ledger)==1)
            return MESResult.success(ledger);
        return MESResult.error();
    }

    @Override
    public MESResult updateLedger(Ledger ledger) {
        Ledger temp = machineLedgerMapper.selectByCode(ledger.getMachineCode());
        if (temp != null && !temp.getMachineCode().equals(ledger.getMachineCode()))
            return MESResult.error();
        else
            machineLedgerMapper.updateLedger(ledger);
        return MESResult.success();
    }

    @Override
    public MESResult selectById(Long machineId) {
        return MESResult.success(machineLedgerMapper.selectById(machineId));
    }

    @Override
    public MESResult updateLedgerStatus(Long machineId) {
        machineLedgerMapper.updateLedgerStatus(machineId);
        return MESResult.success(200);
    }

    @Override
    public MESResult selectByDelete(int status) {
        return MESResult.success(machineLedgerMapper.selectByDelete(status));
    }

    @Override
    public MESResult updateByDelete(Long machineId) {
        machineLedgerMapper.updateByDelete(machineId);
        return MESResult.success(200);
    }

    @Override
    public MESResult selectByCode(String machineCode) {
        Ledger temp = machineLedgerMapper.selectByCode(machineCode);
        return MESResult.success(temp);
//        return MESResult.success(machineLedgerMapper.selectByCode(machineCode));
    }

    @Override
    public MESResult selectWorkshop(Long workshopId) {
        return MESResult.success(machineLedgerMapper.selectWorkshop(workshopId));
    }
}
